<template>
  <Modal v-model="modal" :title="title">
    <div class="tag-modal">
        <Form :model="form" :label-width="110" ref="form" :rules="rules">
            <FormItem label="类型名称" prop="name">
                <Input :maxlength="8" v-model.trim="form.name" placeholder="请输入类型名称"/>
            </FormItem>
             <FormItem label="是否为建店门店">
               <RadioGroup v-model="form.isSpecial">
                <Radio :label="true">是</Radio>
                <Radio :label="false">否</Radio>
            </RadioGroup>
            </FormItem >
            <FormItem label="是否计算积分" :label-width="160">
               <RadioGroup v-model="form.isCalculateIntegral">
                <Radio :label="true">是</Radio>
                <Radio :label="false">否</Radio>
              </RadioGroup>
            </FormItem >
            <FormItem label="是否计算返利" :label-width="160">
               <RadioGroup v-model="form.isCalculateRebate">
                <Radio :label="true">是</Radio>
                <Radio :label="false">否</Radio>
              </RadioGroup>
            </FormItem >
            <FormItem label="是否计算基金" :label-width="160">
               <RadioGroup v-model="form.isCalculateFund">
                <Radio :label="true">是</Radio>
                <Radio :label="false">否</Radio>
              </RadioGroup>
            </FormItem>
            <FormItem label="是否入库后直接出库" :label-width="160">
               <RadioGroup v-model="form.isBarcodeStoreOut">
                <Radio :label="true">是</Radio>
                <Radio :label="false">否</Radio>
              </RadioGroup>
            </FormItem>
            <FormItem :label-width='40'>
              <div class="common-class">
                <i class="ivu-icon ivu-icon-ios-alert"></i>
                <span>  
                  提示：<br> 
                  1、是否为建店门店, 如果选了是，则这个类型的门店不会因为公共资源库的规则而进入公共资源库 <br>
                        2、新店申请只能为“非建店门店” <br>
                        3、是否计算积分/返利/基金, 如果选了否，则这个类型的门店不会计算积分/返利/基金，即使在对应的政策里设置了这个门店类型也不给，以这里的设置为最优先
                </span>
                </div>
            </FormItem>
        </Form>
        
    </div>
    <div slot="footer">
        <Button @click="modal = false" size="large" type="text">取消</Button>
        <Button :disabled="btnDisabled" @click="onSave" type="primary" size="large">确定</Button>
    </div>
</Modal>
</template>
<script>
export default {
  data() {
    
    return {
      id: null,
      modal: false,
      title: '新增门店类型',
      btnDisabled: false,
      rules: {
        name: [
          { required: true, message:'名称不能为空',trigger: "blur" }
        ]
      },
      form: {
        name: "",
        isSpecial: false,
        isBarcodeStoreOut: false
      },
    };
  },
  methods: {
    resetForm() {
      this.$refs.form.resetFields();
      this.form = {
        name: "",
        isSpecial: false,
        isCalculateIntegral:false,
        isCalculateFund:false,
        isCalculateRebate:false,
        isBarcodeStoreOut: false
      };
      this.id = ''
      this.btnDisabled = false;
      this.title = '新增门店类型'
    },
    updateForm(row) {
      this.resetForm()
      this.id = null
      if (row) {
        this.id = row.id;
        this.form.name = row.name
        this.form.isSpecial = row.isSpecial
        this.form.isCalculateIntegral = row.isCalculateIntegral
        this.form.isCalculateFund = row.isCalculateFund
        this.form.isCalculateRebate = row.isCalculateRebate
        this.form.isBarcodeStoreOut = row.isBarcodeStoreOut
        this.title = '编辑门店类型'
      }
      this.openModal()
    },
    openModal() {
      this.modal = true;
    },
    closeModal() {
      this.modal = false;
    },
    onSave() {
      if (this.btnDisabled) return
      this.btnDisabled = true
      this.$refs["form"].validate(valid => {
        if (valid) {
          let text = this.id ? '编辑' : '新增';
          let url = this.id ? '/storeType/modify' : '/storeType/save';
          let param = {
            name: this.form.name,
            isSpecial: this.form.isSpecial,
            isCalculateIntegral: this.form.isCalculateIntegral,
            isCalculateFund: this.form.isCalculateFund,
            isCalculateRebate: this.form.isCalculateRebate,
            isBarcodeStoreOut: this.form.isBarcodeStoreOut
          }
          if (this.id) param.id = this.id
          this.$jsonPost(url, param).then(data => {
            this.$Notice.success({
              title: `${text}成功`
            })
            this.$emit('complete')
            this.closeModal()
          }).catch(() => {
            this.btnDisabled = false
          })
        } else {
          this.btnDisabled = false
        }
      });
    }
  }
}
</script>
<style scoped>
.ivu-radio-default{
  line-height: 4px;
}
</style>